﻿#region << 文 件 说 明 >>
/*----------------------------------------------------------------
// 文件名称：Enum2StringConverter
// 创 建 人：YangCheng
// 创建时间：2022/5/24 13:10:33
// 文件版本：V1.0.0
// ===============================================================
// 功能描述：
//		
//
//----------------------------------------------------------------*/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace V.CodeGenerator.View.Converter
{
    /// <summary>
    /// 
    /// </summary>
    public class Enum2StringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value==null)
            {
                return "";
            }

            var type = value.GetType();
            var field = type.GetField(value.ToString());
            var obj = (DisplayAttribute)field.GetCustomAttribute(typeof(DisplayAttribute));
            return obj.Name ?? "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
